package org.jeecg.modules.basicWarehouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.basicWarehouse.entity.BasicWarehouse;
import org.jeecg.modules.basicWarehouse.service.IBasicWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 仓库
 * @Author: jeecg-boot
 * @Date: 2024-05-09
 * @Version: V1.0
 */
@Api(tags = "仓库")
@RestController
@RequestMapping("/basicWarehouse/basicWarehouse")
@Slf4j
public class BasicWarehouseController extends JeecgController<BasicWarehouse, IBasicWarehouseService> {
    @Autowired
    private IBasicWarehouseService basicWarehouseService;

    /**
     * 分页列表查询
     *
     * @param basicWarehouse
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "仓库-分页列表查询")
    @ApiOperation(value = "仓库-分页列表查询", notes = "仓库-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BasicWarehouse basicWarehouse,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BasicWarehouse> queryWrapper = QueryGenerator.initQueryWrapper(basicWarehouse, req.getParameterMap());
        Page<BasicWarehouse> page = new Page<BasicWarehouse>(pageNo, pageSize);
        IPage<BasicWarehouse> pageList = basicWarehouseService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 列表查询
     *
     */
    @AutoLog(value = "仓库-列表查询")
    @ApiOperation(value = "仓库-列表查询", notes = "仓库-列表查询")
    @GetMapping(value = "/getWarehouseList")
    public Result<?> getWarehouseList() {
        return Result.OK(basicWarehouseService.list());
    }

    /**
     *   添加
     *
     * @param basicWarehouse
     * @return
     */
    @AutoLog(value = "仓库-添加")
    @ApiOperation(value = "仓库-添加", notes = "仓库-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BasicWarehouse basicWarehouse) {
        this.checkColumns(basicWarehouse);
        basicWarehouseService.save(basicWarehouse);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param basicWarehouse
     * @return
     */
    @AutoLog(value = "仓库-编辑")
    @ApiOperation(value = "仓库-编辑", notes = "仓库-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BasicWarehouse basicWarehouse) {
        this.checkColumns(basicWarehouse);
        basicWarehouseService.updateById(basicWarehouse);
        return Result.OK("编辑成功!");
    }

    public void checkColumns(BasicWarehouse basicWarehouse) {
        Assert.isTrue(StringUtils.isNotBlank(basicWarehouse.getWarehouseCode()), "请输入仓库编码");
        Assert.isTrue(StringUtils.isNotBlank(basicWarehouse.getWarehouseName()), "请输入仓库名称");

        QueryWrapper<BasicWarehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(null != basicWarehouse.getId(), "id", basicWarehouse.getId());
        queryWrapper.eq("warehouse_code", basicWarehouse.getWarehouseCode());
        int repeatCount = basicWarehouseService.count(queryWrapper);
        Assert.isTrue(repeatCount == 0, "仓库编码已存在");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "仓库-通过id删除")
    @ApiOperation(value = "仓库-通过id删除", notes = "仓库-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        basicWarehouseService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "仓库-批量删除")
    @ApiOperation(value = "仓库-批量删除", notes = "仓库-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.basicWarehouseService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "仓库-通过id查询")
    @ApiOperation(value = "仓库-通过id查询", notes = "仓库-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BasicWarehouse basicWarehouse = basicWarehouseService.getById(id);
        Assert.notNull(basicWarehouse, "仓库数据异常,请联系管理员");
        return Result.OK(basicWarehouse);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param basicWarehouse
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BasicWarehouse basicWarehouse) {
        return super.exportXls(request, basicWarehouse, BasicWarehouse.class, "仓库");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BasicWarehouse.class);
    }

}
